function ReservationManager(options){

    var defaultOptions = {
        target: "body",
        reservedSeats: [],
        userSeats: [],
        orderedSeats: [],
        reservationMode: "user",
        reservationsPerUser: 4,
        paidSeats:[]
    };

    this.state = $.extend(true, defaultOptions, options);
    this.state.target = $(this.state.target);
}

ReservationManager.prototype.updateReservationCount = function(){
    $("#freeReservationCount").text(this.state.reservationsPerUser - this.state.userSeats.length);
}

ReservationManager.prototype.clear = function (){
    this.state.target.empty();
};

ReservationManager.prototype.load = function (){
    var target = this.state.target;
    this.clear();
    var instance = this;
    $.get("./resources/tables.json", {},
        function(data){
            for(var i = 0; i < data.length; i++){
                var planItem = data[i];
                var state = planItem.state;
                state.top = parseInt(planItem.position.top);
                state.left = parseInt(planItem.position.left);
                var type = planItem.type;

                var item;
                switch(type){
                    case ROUND_TABLE:
                        item = new RoundTable(state);
                        break;
                    case SEAT:
                        item = new Seat(state);
                        break;
                    case CUSTOM_OBJECT:
                        item = new CustomObject(state);
                        break;
                    default:
                        continue;
                }

                item.appendTo(target);
            }
            instance.update();
        }
    );
};

ReservationManager.prototype.update = function (){
    var instance = this;
    this.state.target.find(".reservable").each(function(){
        var element = $(this);
        element.unbind("click");
        var type = element.attr("data-type");
        var id = element.attr("id");
        if (type == SEAT){
            element.css("background", "");
            element.removeClass("free");
            element.removeClass("reservedByUser");
            element.removeClass("reserved");
            element.removeClass("orderedByUser");
            element.removeClass("paid");

            if (instance.state.paidSeats.indexOf(id) >= 0) {
                element.addClass("paid");
            }
            else if (instance.state.orderedSeats.indexOf(id) >= 0){
                element.addClass("orderedByUser");
            } else if (instance.state.userSeats.indexOf(id) >= 0){
                element.addClass("reservedByUser");
            } else if (instance.state.reservedSeats.indexOf(id) >= 0){
                element.addClass("reserved");
            } else {
                element.addClass("free");
            }

            element.bind("click", function(event){
                instance.clickSeat(element, id);
                event.stopPropagation();
                return false;
            });
        } else if (type == ROUND_TABLE){
            element.click(function(){
                instance.reserveTable(element, id);
            });
        }
    });
    this.updateReservationCount();
};

ReservationManager.prototype.clickSeat = function (element, seatId){
    if (this.state.reservationMode != "admin")
    {
        if (element.hasClass("free")){
            this.reserveSeat(element, seatId);
        } else if (element.hasClass("reservedByUser")){
            this.unreserveSeat(element, seatId);
        }
    }
    else
    {
        this.showReservation(element,seatId);
    }
};

ReservationManager.prototype.showReservation = function(element,seatId){
    $.ajax({
        url: "svc/reserve.php",
        dataType: "JSON",
        type: "POST",
        data: {
            action: "getReserved"

        },
        success : function(data){
            alert(data);
        }
    });
}


ReservationManager.prototype.unreserveSeat = function (element, seatId){
    var instance = this;

    $.ajax({
        url: "svc/reserve.php",
        dataType: "JSON",
        type: "POST",
        data: {
            action: "unreserveSeat",
            id: seatId
        },
        success : function(data){
            if (data.success){
                instance.state.userSeats = data.data;
                var reserved = [];
                var seats = instance.state.reservedSeats;
                for(var i in seats){
                    if (seatId !== seats[i]){
                        reserved.push(seats[i]);
                    }
                }
                instance.state.reservedSeats = reserved;
                instance.update();
            } else {
                alert(data.message);
            }
        }
    });
};

ReservationManager.prototype.reserveSeat = function (element, seatId){
    var instance = this;
    if (this.state.userSeats.length >= this.state.reservationsPerUser){
        alert("Prekročili ste maximálny počet rezervovaných miest");
    } else {
        $.ajax({
            url: "svc/reserve.php",
            dataType: "JSON",
            type: "POST",
            data: {
                action: "reserveSeat",
                id: seatId
            },
            success : function(data){
                if (data.success){
                    instance.state.userSeats = data.data;
                    instance.update();
                } else {
                    alert(data.message);
                    instance.refresh();
                }
            }
        });
    }
};

ReservationManager.prototype.reserveTable = function (element, tableId){
    var instance = this;
    $.ajax({
        url: "svc/reserve.php",
        dataType: "JSON",
        type: "POST",
        data: {
            action: "reserveTable",
            id: tableId
        },
        success : function(data){
            if (data.success){
                instance.state.userSeats = data.data;
                instance.update();
            } else {
                alert(data.message);
                instance.refresh();
            }
        }
    });
};

ReservationManager.prototype.magic = function (){
    var instance = this;
    $.ajax({
        url: "svc/reserve.php",
        dataType: "JSON",
        type: "POST",
        data: {
            action: "magic"
        },
        success : function(data){
            if (data.success){
                instance.state.userSeats = data.data;
                instance.update();
            } else {
                alert(data.message);
            }
        }
    });
};

ReservationManager.prototype.order = function (){
    var instance = this;
    $.ajax({
        url: "svc/reserve.php",
        dataType: "JSON",
        type: "POST",
        data: {
            action: "order"
        },
        success : function(data){
            if (data.success){
                instance.state.orderedSeats = data.data;
                instance.update();
            } else {
                alert(data.message);
            }
        }
    });
};

ReservationManager.prototype.refresh = function(){
    var instance = this;
    $.ajax({
        url: "svc/reserve.php",
        dataType: "JSON",
        type: "POST",
        data: {
            action: "refresh"
        },
        success : function(data){
            if (data.success){
                instance.state.userSeats = data.userSeats;
                instance.state.orderedSeats = data.orderedSeats;
                instance.state.reservedSeats = data.reservedSeats;
                instance.state.paidSeats = data.paidSeats;

                instance.update();
            } else {
                alert(data.message);
            }
        }
    });
}
